//index.js
import SessionUtil from '../../../data/session/session'
const Common = require('../../../utils/common.js');
var a = '../../images/radio-checked.png';
var b = '../../images/radio.png'
Page({
    data: {
        token: '',
        isExpress: 'active',
        isSince: '',
        imgRadio1: a,
        imgRadio2: b,
        invoiceType: '无',
        invoCustName: '',
        invoiceTypeList: {}
    },
    onLoad: function (options) {
        SessionUtil.getInstance().then(session=>{
            let invoiceType = options.invoiceType ? options.invoiceType : this.data.invoiceType,
            invoCustName = options.invoCustName ? options.invoCustName : this.data.invoCustName
            this.setData({
                token: session.token,
                invoiceType: invoiceType,
                invoCustName: invoCustName
            });
            this.loadInvoiceType();
            wx.setNavigationBarTitle({
                title: '发票信息'
            })
        })
    },

    loadInvoiceType: function () {
        var invoiceTypeList = [{ id: 1, name: '不开具发票' }, { id: 2, name: '个人' }, { id: 3, name: '公司' }];
        for (var i = 0; i < invoiceTypeList.length; i++) {
            if (this.data.invoiceType == invoiceTypeList[i].id) {
                invoiceTypeList[i].src = a
            } else {
                invoiceTypeList[i].src = b
            }
        }
        this.setData({
            invoiceTypeList: invoiceTypeList
        });
    },
    switchInvoiceType: function (e) {
        var invoiceType = e.currentTarget.id;
        var _invoiceTypeList = this.data.invoiceTypeList;
        for (var i = 0; i < _invoiceTypeList.length; i++) {
            if (invoiceType == _invoiceTypeList[i].id) {
                _invoiceTypeList[i].src = a
            } else {
                _invoiceTypeList[i].src = b
            }
        }
        this.setData({
            invoiceTypeList: _invoiceTypeList,
            invoiceType: invoiceType
        });
    },
    bindInvoCustName: function (e) {
        this.setData({
            invoCustName: e.detail.value
        });
    },
    addInvoice: function (e) {

        var s_invoiceType = this.data.invoiceType;
        var s_invoCustName = this.data.invoCustName;
        if (s_invoiceType != 1 && !s_invoCustName) {
            Common.Alert("非空验证", "请输入发票信息抬头!");
            return false;
        }
        if (s_invoiceType == 1) {
            s_invoCustName = '';
        }
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2]; //上一个页面
        prevPage.setData({
            invoiceType: s_invoiceType,
            invoCustName: s_invoCustName,
        })
        wx.navigateBack();
    },

    onPullDownRefresh: function () {
        wx.stopPullDownRefresh()
    },

})